﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Repository;
using Ext.Net;
using ExaminationSys.Helper;
using ExaminationSys.DataAccess.Entity;


namespace ExaminationSys.View.Paper
{
    public partial class PaperInfoEdit : System.Web.UI.UserControl
    {
        private static readonly PaperInfoService Proxy = ObjectFactory<PaperInfoService>.Create();
        private static readonly SectionInfoService ProxySectionInfo = ObjectFactory<SectionInfoService>.Create();
        private static PaperInfo entity = new PaperInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                entity = null;

                BindControl();
            }
        }
     
        public Store GridStore { get; set; }
        public string ID { get; set; }

        public void BindControl()
        {
            var studySectionList = ProxySectionInfo.GetStudySectionList();
            foreach (var s in studySectionList)
            {
                Ext.Net.ListItem item = new Ext.Net.ListItem();         //每次创建一个Ext.Net.ListItem的对象
                item.Value = s.ID.ToString();
                item.Text = s.Name;
                SectionID.Items.Add(item);
            }

        }

        public void SetData(string id)
        {
            PaperInfoEditPanel.Reset();
            if (!string.IsNullOrEmpty(id))
            {
                var realId = new Guid(id);
                var PaperInfo = Proxy.Get(realId);

                if (PaperInfo != null)
                {
                    BindControl();
                    entity = PaperInfo;
                    Title.Text = PaperInfo.Title;
                    
                    Description.Text = PaperInfo.Description;
                    SectionID.Select(PaperInfo.SectionID);
                    Level.SetValue(PaperInfo.Level.ToString());
                    Degree.SetValue(PaperInfo.Degree.ToString());
                    Type.SetValue(PaperInfo.Type.ToString());
                    Score.Value=            PaperInfo.Score ;
                    Timeout.Value=          PaperInfo.Timeout ;
                    Description.Value = PaperInfo.Description;
                    HidPaperFeedBackCommentID.Value = PaperInfo.PaperFeedBackCommentID.ToString();
                    Comment.Text = PaperInfo.PaperFeedBackCommentName;
                    
                    if (PaperInfo.ID != null)
                    {
                        IDHidden.Value = PaperInfo.ID.ToString();
                        X.Js.Call("setID", PaperInfo.ID, PaperInfo.PaperFeedBackCommentID.ToString());
                    }

                    SaveButton.Text = "下一步";
                }
            }
            else
            {
                entity = new PaperInfo();
                Type.SetValue("1");
                Level.SetValue("3");
                Degree.SetValue("3");
                Score.Value = "100";
                Timeout.Value = "90";
               
                

            }
        }

        public void Show()
        {
            this.PaperInfoEditWindow.Show();
        }

       

        protected void SavePaperInfo(object sender, DirectEventArgs e)
        {

            string ID          = IDHidden.Value.ToString();

            string title = Title.Text;
            if (string.IsNullOrEmpty(title))
            {
                MessageBoxHelper.ShowWarning("标题不能为空");
                return;
            }

            if (SectionID.SelectedItem == null || SectionID.SelectedItem.Value.ToString()=="")
            {
                MessageBoxHelper.ShowWarning("栏目不能为空");
                return;
            }
            string sectionID = SectionID.SelectedItem.Value.ToString();

            string sectionName = SectionID.Value.ToString(); 
            string paperNo     = DateTime.Now.ToString("yyyyMMddhhmmss");

            if (Level.SelectedItem== null || Level.SelectedItem.Value.ToString() == "")
            {
                MessageBoxHelper.ShowWarning("阶段不能为空");
                return;
            }
            string level       = Level.SelectedItem.Value.ToString();

            if (Degree.SelectedItem == null || Degree.SelectedItem.Value.ToString() == "")
            {
                MessageBoxHelper.ShowWarning("难度不能为空");
                return;
            }
            string degree      = Degree.SelectedItem.Value.ToString();

            if (Type.SelectedItem== null || Type.SelectedItem.Value.ToString() == "")
            {
                MessageBoxHelper.ShowWarning("类型不能为空");
                return;
            }
            string type        = Type.SelectedItem.Value.ToString();


            int score ;
            if (Score.Value==null || !int.TryParse(Score.Value.ToString(),out score))
            {
                MessageBoxHelper.ShowWarning("总分请填写数字");
                return;
            };
            int timeout ;
            if (Timeout.Value == null || !int.TryParse(Timeout.Value.ToString(), out timeout))
            {
                MessageBoxHelper.ShowWarning("测试时间请填写数字");
                return;
            }

            if (HidPaperFeedBackCommentID.Value == string.Empty || Comment.Text.Trim()==string.Empty)
            {
                MessageBoxHelper.ShowWarning("请选择评语");
                return;
            }

            entity.PaperFeedBackCommentID = new Guid(HidPaperFeedBackCommentID.Value);
            entity.PaperFeedBackCommentName = Comment.Text;

            string description = Description.Value.ToString();
            DateTime OpeatorTime = DateTime.Now;
            int status = 1;



            entity.SectionID = new Guid(SectionID.SelectedItem.Value);
            entity.SectionName = sectionName;
            entity.PaperNo = paperNo;
            entity.Title = title;
            entity.Score = score;
            entity.Timeout = timeout;

            int templevel;
            if(!int.TryParse(level, out templevel)){
                MessageBoxHelper.ShowWarning("阶段不能为空");
                return;
            }
            entity.Level = templevel;

            int tempDegree;
            if (!int.TryParse(degree, out tempDegree))
            {
                MessageBoxHelper.ShowWarning("难度不能为空");
                return;
            }
            entity.Degree = tempDegree;

            int tempType;
            if (!int.TryParse(type, out tempType))
            {
                MessageBoxHelper.ShowWarning("类型不能为空");
                return;
            }
            entity.Type = tempType;
            entity.Description = description;
            entity.Status = status;

            var op = FormsAuthenticationHelper.GetCurrentUserID();
            if (string.IsNullOrEmpty(op))
            {
                FormsAuthenticationHelper.SignOut();
                Response.Redirect("~/Account/Login.aspx");
            }
            entity.Operator = op;

            entity.OperatorTime = DateTime.Now;
            var sucess = false;
            // entity.
            if (string.IsNullOrWhiteSpace(IDHidden.Value))
            {
                entity.ID = Guid.NewGuid();
                var retCode = Proxy.Insert(entity);
                sucess = retCode != null;
            }
            else
            {
                sucess = Proxy.Update(entity);
            }

            if (sucess)
            {
                //MessageBoxHelper.ShowInfo("保存成功");
                //GridStore.Reload();
                //PaperInfoEditWindow.Hide();

                //固定=1，自动=2
                if (Type.SelectedItem.Value.ToString() == "1")
                {
                    Response.Redirect("PaperDetailFixedList.aspx?PaperID=" + entity.ID.ToString() + "&Title=" + entity.Title);
                    return;
                }
                else
                {
                    Response.Redirect("PaperDetailAutoList.aspx?PaperID="+ entity.ID.ToString() + "&Title=" + entity.Title);
                    return;
                }

            }
            else
            {
                MessageBoxHelper.ShowInfo("保存失败");
                return;

            }

        


        }

    }
}